package com.starblink.library.widget.novice_guide.product

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.view.*
import com.skydoves.balloon.BalloonGuide
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.ext.toBitmap2LayoutEnhanced
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.event.TrackEvent
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.util.FlowCountDownTimer
import com.starblink.basic.style.R
import com.starblink.library.widget.databinding.AddVProduct1ToastBinding

object ProductFindSimilarPopViewV2 {

    private val downTimer = FlowCountDownTimer(time = 10)
    private var anchorBitmap: Bitmap? = null
    private var show = false

    @SuppressLint("ClickableViewAccessibility")
    fun pushPopWindow(
        anchorView: View,
        parent: ViewGroup,
        dismissCallBack: (() -> Unit)? = null
    ) {
        //↓
        val toastLay =
            AddVProduct1ToastBinding.inflate(
                LayoutInflater.from(anchorView.context),
                parent,
                false
            )
//        toastLay.btm.enableCorner(32.dp)
        downTimer.start(callBack = {
            anchorBitmap = anchorView.toBitmap2LayoutEnhanced()
            if (anchorBitmap != null && (anchorBitmap!!.width / 330.dp) == 1) {//351取个小于它的值
                toastLay.btm.setImageBitmap(anchorBitmap)
                if (!show) {
                    show = true
                    var balloon = BalloonGuide.Builder(parent.context)
                        .setLayout(toastLay)
                        .setPadding(0)
                        .setOnBalloonDismissListener {
                            dismissCallBack?.invoke()
                        }.setOnBalloonClickListener {
                            SpmTrackHandler.addSingleTrackData(
                                SkAntEntity(
                                    spmCnt = GTrackerAssistUtils.fetchElementValue(
                                        SpmPageDef.SimilarProductListPage,
                                        SpmElementDef.Element_33069
                                    ), params = mapOf("function" to "longStress")
                                )
                            )
                        }
                        .setBackgroundColorResource(R.color.sk_gray900_opacity70)
                        .build()
                    balloon.showFull(parent)
//                    parent.postDelayed(3000L) {
//                        if (balloon.isShowing) {
//                            balloon.dismiss()
//                        }
//                    }
                }
            }
        }, onFinish = { })
        //↑
        SpmTrackHandler.addSingleTrackData(
            SkAntEntity(
                event = TrackEvent.elementExposure.value,
                spmCnt = GTrackerAssistUtils.fetchElementValue(
                    SpmPageDef.SimilarProductListPage,
                    SpmElementDef.Element_33069
                ), params = mapOf("function" to "longStress")
            )
        )
    }
}

